package com.small.nacos.config.server.controller;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.StringUtils;

import small.common.core.util.ParamUtil;
import com.small.nacos.config.server.constant.ConfigConstants;
import com.small.nacos.config.server.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Author zhoujin
 * @Date 2022/11/22 17:37
 */
@RestController
@RequestMapping(ConfigConstants.CONFIG_CONTROLLER_PATH)
public class ConfigController {

    private final ConfigServletInner inner;

    @Autowired
    public ConfigController(ConfigServletInner configServletInner) {
        this.inner = configServletInner;
    }

    @GetMapping
    public void getConfig(HttpServletRequest request, HttpServletResponse response,
                          @RequestParam("dataId") String dataId, @RequestParam("group") String group,
                          @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
                          @RequestParam(value = "tag", required = false) String tag)
            throws IOException, ServletException, NacosException {
        ParamUtil.checkParam(tenant, 16);
        ParamUtil.checkParam(Arrays.asList(dataId, group, tag));
        final String clientIp = RequestUtil.getRemoteIp(request);
        inner.doGetConfig(request, response, dataId, group, tenant, tag, clientIp);
    }

}
